package com.some;

import java.lang.String;

import java.util.Scanner;

public class BaiDu_1 {
    public int sortABC(String str) {
        char[] nums = str.toCharArray();
        if (nums.length == 1) {
            return 0;
        }
        int sum = 0, b = nums.length - 1;
        int i = 0, j = 0;
        char tmp;
        for (int k = 0; k <= b - j; k++) {
            if (nums[k] == 'A' && k > i) {
                tmp = nums[k];
                nums[k] = nums[i];
                nums[i] = tmp;
                i++;
                k--;
                sum++;
            } else if (nums[k] == 'C' && b - j > k) {
                tmp = nums[k];
                nums[k] = nums[b - j];
                nums[b - j] = tmp;
                j++;
                k--;
                sum++;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int q = scanner.nextInt();
        String[] strings = new String[q];
        for (int i = 0; i < q; i++) {
            strings[i] = scanner.next();
        }
        BaiDu_1 baiDu_1 = new BaiDu_1();
        for (String string : strings) {
            System.out.println(baiDu_1.sortABC(string));
        }
    }
}
